package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ManageService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private VideoApi videoApi;
    @DubboReference
    private MovementApi movementApi;
    @DubboReference
    private CommentApi commentApi;

    public PageResult fandAll(Integer page, Integer pagesize) {
        //调用API查询所有
        IPage<UserInfo> iPage = (IPage<UserInfo>) userInfoApi.finAll(page, pagesize);
        return new PageResult(page, pagesize, Math.toIntExact(iPage.getTotal()), iPage.getRecords());
    }

    //根据ID查询用户信息
    public UserInfo finUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        return userInfo;
    }

    //分页查询个人视频列表
    public PageResult findByIdVideos(Integer page, Integer pagesize, Long uid) {
        PageResult pr = videoApi.findVideosByVid(page, pagesize, uid);
        return pr;
    }

    //分页查询个人动态列表
    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        PageResult result = movementApi.findByUserIds(uid,state, page, pagesize);
        List<Movement> items = (List<Movement>) result.getItems();
        List<Long> userIds = CollUtil.getFieldValues(items, "userId", Long.class);
        if (CollUtil.isEmpty(items)){
            return new PageResult();
        }
        Map<Long, UserInfo> mp = userInfoApi.findByIds(userIds, null);
        ArrayList<MovementsVo> vos = new ArrayList<>();
        for (Movement item : items) {
            UserInfo userInfo = mp.get(item.getUserId());
            if (userInfo!=null){
                MovementsVo vo = MovementsVo.init(userInfo, item);
                vos.add(vo);
            }
        }
        result.setItems(vos);
        return result;
    }

    //根据动态ID查询
    public MovementsVo findByidMovements(String id) {
        Movement movement = movementApi.findById(id.toString());
        if (movement!=null){
            UserInfo userInfo = userInfoApi.findById(movement.getUserId());
            if (userInfo!=null){
                MovementsVo vo = MovementsVo.init(userInfo, movement);
                return vo;
            }
        }
        return new MovementsVo();
    }

    //分页查询评论列表
    public PageResult findmessageID(Integer page, Integer pagesize, String messageID) {
        List<Comment> comments = commentApi.findComments(messageID, CommentType.COMMENT, page, pagesize);
        if (CollUtil.isEmpty(comments)){
           return new PageResult();
        }
        List<Long> userId = CollUtil.getFieldValues(comments, "userId", Long.class);
        Map<Long, UserInfo> mp = userInfoApi.findByIds(userId, null);
        ArrayList<CommentVo> vos = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = mp.get(comment.getUserId());
            if (userInfo!=null){
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,vos.size(),vos);
    }
}
